/*
 * 文 件 名:  ResourceInitLoad.java
 * 版    权:  Huawei Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  admin
 * 修改时间:  2013-6-29
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package org.gfz.frame.core.resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.gfz.frame.core.resource.ConfResource.ConfType;
import org.gfz.frame.core.resource.DataRersource.DataType;
import org.gfz.frame.core.resource.I18NResource.I18NType;
import org.gfz.frame.core.resource.model.ConfResourceModel;
import org.gfz.frame.core.resource.model.DataResourceModel;
import org.gfz.frame.core.resource.model.I18NResourceModel;
import org.gfz.frame.core.resource.model.ResourceModel;

/**
 * <一句话功能简述>资源初始化加载类 <功能详细描述>
 * 
 * @author 姓名 工号
 * @version [版本号, 2013-6-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ResourceInitLoad
{
    /**
     * <一句话功能简述>资源类型 <功能详细描述>
     * 
     * @author 姓名 工号
     * @version [版本号, 2013-6-29]
     * @see [相关类/方法]
     * @since [产品/模块版本]
     */
    public static enum ResourceType
    {
        CONF, I18N, DATA
    }
    
    /**
     * w
     */
    private static Map<String, List<ResourceModel>> resMap = new HashMap<String, List<ResourceModel>>();
    
    /**
     * <一句话功能简述> <功能详细描述>
     * 
     * @param respro
     * @see [类、类#方法、类#成员]
     */
    public synchronized static void init(Properties respro)
    {
        if (null == respro || respro.isEmpty())
        {
            return;
        }
        
        Enumeration<?> keys = respro.keys();
        String key = null;
        String[] keyArr = null;
        
        while (keys.hasMoreElements())
        {
            key = (String)keys.nextElement();
            System.out.println("key=" + key);
            keyArr = key.split("\\.");
            
            if (null == keyArr || keyArr.length == 0)
            {
                continue;
            }
            
            // conf类型资源，加载
            if (keyArr[0].equalsIgnoreCase(ResourceType.CONF.name()))
            {
                loadConfResource(keyArr, respro.getProperty(key));
            }
            
            // 加载i18n类型资源
            if (keyArr[0].equalsIgnoreCase(ResourceType.I18N.name()))
            {
                loadI18NResource(keyArr, respro.getProperty(key));
            }
            
            // 加载data类型资源
            if (keyArr[0].equalsIgnoreCase(ResourceType.DATA.name()))
            {
                loadDataResource(keyArr, respro.getProperty(key));
            }
        }
    }
    
    /**
     * 
     * <一句话功能简述> <功能详细描述>
     * 
     * @see [类、类#方法、类#成员]
     */
    public static void clear()
    {
        resMap.clear();
    }
    
    /**
     * <一句话功能简述> <功能详细描述>
     * 
     * @param resType
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected static List<ResourceModel> getResource(ResourceType resType)
    {
        List<ResourceModel> list = null;
        
        switch (resType)
        {
            case CONF:
                list = resMap.get(ResourceType.CONF.name());
                break;
            case I18N:
                list = resMap.get(ResourceType.I18N.name());
                break;
            case DATA:
                list = resMap.get(ResourceType.DATA.name());
                break;
            default:
                break;
        }
        
        return list;
    }
    
    /**
     * <一句话功能简述> <功能详细描述>
     * 
     * @param keyArr
     * @param value
     * @see [类、类#方法、类#成员]
     */
    private static void loadConfResource(String[] keyArr, String value)
    {
        if (keyArr.length != 3)
        {
            return;
        }
        
        ConfResourceModel model = null;
        
        if (keyArr[1].equalsIgnoreCase(ConfType.KEY_VALUE.name()))
        {
            model = new ConfResourceModel(ResourceType.CONF, ConfType.KEY_VALUE);
            model.setResId(keyArr[2]);
            model.setResPath(value);
        }
        
        if (null == model)
        {
            return;
        }
        
        List<ResourceModel> list = resMap.get(ResourceType.CONF.name());
        
        if (null == list)
        {
            list = new ArrayList<ResourceModel>();
        }
        
        list.add(model);
        resMap.put(ResourceType.CONF.name(), list);
    }
    
    private static void loadI18NResource(String[] keyArr, String value)
    {
        if (keyArr.length != 3)
        {
            return;
        }
        
        I18NResourceModel model = null;
        
        if (keyArr[1].equalsIgnoreCase(I18NType.EN_US.name()))
        {
            model = new I18NResourceModel(ResourceType.I18N, I18NType.EN_US);
            model.setResId(keyArr[2]);
            model.setResPath(value);
        }
        
        if (keyArr[1].equalsIgnoreCase(I18NType.ZH_CN.name()))
        {
            model = new I18NResourceModel(ResourceType.I18N, I18NType.ZH_CN);
            model.setResId(keyArr[2]);
            model.setResPath(value);
        }
        
        if (null == model)
        {
            return;
        }
        
        List<ResourceModel> list = resMap.get(ResourceType.I18N.name());
        
        if (null == list)
        {
            list = new ArrayList<ResourceModel>();
        }
        
        list.add(model);
        resMap.put(ResourceType.I18N.name(), list);
    }
    
    private static void loadDataResource(String[] keyArr, String value)
    {
        if (keyArr.length != 3 || null == value)
        {
            return;
        }
        
        String[] values = value.split(",");
        List<String> paths = new ArrayList<String>();
        
        for (String temp : values)
        {
            paths.add(temp);
        }
        
        DataResourceModel model = null;
        
        if (keyArr[1].equalsIgnoreCase(DataType.FILE.name()))
        {
            model = new DataResourceModel(ResourceType.DATA, DataType.FILE);
            model.setResId(keyArr[2]);
            model.setPathList(paths);
        }
        
        if (keyArr[1].equalsIgnoreCase(DataType.IMAGE.name()))
        {
            model = new DataResourceModel(ResourceType.DATA, DataType.IMAGE);
            model.setResId(keyArr[2]);
            model.setPathList(paths);
        }
        
        if (null == model)
        {
            return;
        }
        
        List<ResourceModel> list = resMap.get(ResourceType.DATA.name());
        
        if (null == list)
        {
            list = new ArrayList<ResourceModel>();
        }
        
        list.add(model);
        resMap.put(ResourceType.DATA.name(), list);
    }
    
    /**
     * <一句话功能简述> <功能详细描述>
     * 
     * @param args
     * @see [类、类#方法、类#成员]
     */
    public static void main(String[] args)
    {
        InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream("init.properties");
        
        Properties pro = new Properties();
        
        try
        {
            pro.load(in);
            init(pro);
            
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (in != null)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            
        }
        
        System.out.println(resMap);
        
        System.out.println(I18NResource.getInstance().getMessage("helloTip"));
        I18NResource.getInstance().setI18N(I18NType.EN_US);
        System.out.println(I18NResource.getInstance().getMessage("helloTip"));
        
        // System.out.println(ConfResource.getInstance().getValue(".handlers"));
        // System.out.println(ConfResource.getInstance().getValue("log4j",".handlers"));
        // System.out.println(ConfResource.getInstance().getProperties("log4j"));
    }
    
}
